const mongoose = require('mongoose');
//连接数据库
mongoose.connect('mongodb://localhost:27017/test', { useUnifiedTopology: true,useNewUrlParser: true  });

//创建实体
//mongodb只需要在代码中设计数据库
const Cat = mongoose.model('Cat', {name: String});

const schema = new Schema({
    name:    String,//字符串
    binary:  Buffer,//二进制
    living:  Boolean,//布尔
    updated: { type: Date, default: Date.now },//日期
    age:     { type: Number, min: 18, max: 65 },//数字
    mixed:   Schema.Types.Mixed,
    _someId: Schema.Types.ObjectId,
    decimal: Schema.Types.Decimal128,
    array: [],//数组
    ofString: [String],//字符串数组
    ofNumber: [Number],//number数组
    ofDates: [Date],//日期数组
    ofBuffer: [Buffer],
    ofBoolean: [Boolean],
    ofMixed: [Schema.Types.Mixed],
    ofObjectId: [Schema.Types.ObjectId],
    ofArrays: [[]],
    ofArrayOfNumbers: [[Number]],
    nested: {
        stuff: { type: String, lowercase: true, trim: true }
    },//对象
    map: Map, //map对象
    mapOfString: {
        type: Map,
        of: String
    }
})

const Thing = mongoose.model('Thing', schema);

let m = new Thing;
m.name = 'Statue of Liberty';
m.age = 125;
m.updated = new Date;
m.binary = Buffer.alloc(0);
m.living = false;
m.mixed = { any: { thing: 'i want' } };
m.markModified('mixed');
m._someId = new mongoose.Types.ObjectId;
m.array.push(1);
m.ofString.push("strings!");
m.ofNumber.unshift(1,2,3,4);
m.ofDates.addToSet(new Date);
m.ofBuffer.pop();
m.ofMixed = [1, [], 'three', { four: 5 }];
m.nested.stuff = 'good';
m.map = new Map([['key', 'value']]);
m.save(callback);

//实例化
for (let i = 0; i < 100; i++) {
    const kitty = new Cat({name: 'Zildjian ::'+i});
    //保存
    kitty.save().then((res) => console.log(res));
}
